package ${groupId}.${projectName}.service.account;

import com.gzkit.baseframework.core.utils.RequestMediaUtil;
import com.gzkit.baseframework.security.shiro.provider.support.FourAAccountProviderSupport;
import com.gzkit.baseframework.security.vo.Account;
import com.gzkit.baseframework.sys.user.entity.SysUser;
import com.gzkit.baseframework.sys.user.entity.SysUserBase;
import com.gzkit.baseframework.sys.user.service.SysUserBaseService;
import com.gzkit.baseframework.sys.user.vo.SysUserBaseVO;
import com.gzkit.baseframework.sys.user.vo.SysUserDetailVO;
import com.gzkit.baseframework.ws.client.foura.service.SysUserFouraService;
import com.gzkit.baseframework.ws.client.foura.vo.LoginResponseOf4A;
import com.gzkit.baseframework.ws.client.foura.vo.SysUserFouraVO;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用于4a登录 </br>
 * 如果不需要4a登录，该类可以删除 </br>
 * 下面代码仅做示例用途
 */
@Service
public class FourAAccountService extends FourAAccountProviderSupport {

    @Autowired
    private SysUserBaseService userBaseService;

    @Autowired
    private SysUserFouraService userFouraService;

    @Value("${r'${mediaPath}'}")
    private String mediaPath;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Account login(String username, String password) throws IncorrectCredentialsException {
        //根据用户名name查询系统用户信息
        SysUserBaseVO user = new SysUserBaseVO();
        user.setUsername(username);
        user = userBaseService.getOne(user);
        if (user != null) {
            //若4a用户已绑定到系统用户，直接返回详情信息
            return getAccountById(user.getId());
        }else {
            //4a用户初次登陆系统，绑定到系统用户
            //请求主站4a登陆接口，验证4a用户
            LoginResponseOf4A loginResponseOf4A = userFouraService.login(username, password);
            if (loginResponseOf4A.getResultCode() == 1) {
                SysUserFouraVO sysUserFouraVO = LoginResponseOf4A.toSysUserFouraVO(loginResponseOf4A);
                SysUserBase userBase = new SysUserBase();
                userBase.setUsername(username);
                userBase.setPassword(password);
                SysUser sysUser = new SysUser();
                sysUser.setRealname(loginResponseOf4A.getName());
                userBase.setUser(sysUser);
                userBase = userBaseService.saveFourAUser(userBase);
                sysUserFouraVO.setId(userBase.getId());
                userFouraService.save(sysUserFouraVO);
                return getAccountById(userBase.getId());
            }
        }
        return null;
    }

    /**
     * 获取用户详情信息，并转换成Account对象
     * @param userId
     * @return Account
     */
    private Account getAccountById(String userId) {
        SysUserDetailVO detail = userBaseService.getDetailById(userId);
        Account account = new Account();
        account.setUid(detail.getId());
        account.setUsername(detail.getUsername());
        account.setPassword(detail.getPassword());
        account.setSalt(detail.getSalt());
        account.setStatus(detail.getUser().getStatus());
        account.setRealname(detail.getUser().getRealname());
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Map<String, Object> extra = account.getExtra();
        String headImg = RequestMediaUtil.appendUrl(request, mediaPath + "/headImg/" + detail.getId());
        extra.put("headImg", headImg);
        account.setExtra(extra);
        return account;
    }
}
